package com.demo2do.boyle.client.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.boyle.client.cache.accessor.PersistentAccessor;
import com.demo2do.boyle.client.service.CustomerTagService;
import com.demo2do.boyle.common.entity.customer.CustomerTag;
import com.demo2do.boyle.common.entity.system.tag.Tag;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.service.impl.GenericServiceImpl;

/**
 * 
 * @author Wilson
 */
@Service("customerTagService")
public class CustomerTagServiceImpl extends GenericServiceImpl<CustomerTag> implements CustomerTagService {

    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    @Autowired
	private PersistentAccessor persistentAccessor;
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.boyle.client.service.CustomerTagService#listCustomerLifeTag(java.util.List)
     */
	public List<Tag> listCustomerLifeTag(List<Integer> tagCodes) {
		
		List<Tag> customerLifeTags = new ArrayList<Tag>();
		
		Map<String, Object> systemTags =  persistentAccessor.getLifeTags();
		for(String key : systemTags.keySet()) {
			if(tagCodes.contains(Integer.valueOf(key))) {
				customerLifeTags.add((Tag) systemTags.get(key));
			}
		}
		return customerLifeTags;
	}
	

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.client.service.CustomerTagService#listCustomerInterestTag(java.util.List)
	 */
	public List<Tag> listCustomerInterestTag(List<Integer> tagCodes) {
		
		List<Tag> customerinterestTags = new ArrayList<Tag>();
		
		Map<String, Object> systemTags =  persistentAccessor.getInterestTags();
		for(String key : systemTags.keySet()) {
			if(tagCodes.contains(Integer.valueOf(key))) {
				customerinterestTags.add((Tag)systemTags.get(key));
			}
		}
		return customerinterestTags;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.client.service.CustomerTagService#addLifeTag()
	 */
	public CustomerTag addLifeTag(Integer id, int code) {
		
		// onetoone relationship so customerId same as customerTag id
		CustomerTag customerTag = genericDaoSupport.load(CustomerTag.class, id);
		
		// do update
		genericDaoSupport.update(customerTag.addLifeTag(code));
		
		return customerTag;
		
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.client.service.CustomerTagService#addInterestTag()
	 */
	public CustomerTag addInterestTag(Integer id, int code) {
		
		// onetoone relationship so customerId same as customerTag id
		CustomerTag customerTag = genericDaoSupport.load(CustomerTag.class, id);
		
		// do update
		genericDaoSupport.update(customerTag.addInterestTag(code));
		
		return customerTag;
	}

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.client.service.CustomerTagService#deleteLifeTag()
	 */
	public CustomerTag deleteLifeTag(Integer id, int code) {
		
		// onetoone relationship so customerId same as customerTag id
		CustomerTag customerTag = genericDaoSupport.load(CustomerTag.class, id);
		
		// do update
		genericDaoSupport.update(customerTag.deleteLifeTag(code));
		
		return customerTag;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.client.service.CustomerTagService#deleteIntergestTag()
	 */
	public CustomerTag deleteIntergestTag(Integer id, int code) {
		
		// onetoone relationship so customerId same as customerTag id
		CustomerTag customerTag = genericDaoSupport.load(CustomerTag.class, id);

		// do update
		genericDaoSupport.update(customerTag.deleteInterestTag(code));
		
		return customerTag;
	}

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.client.service.CustomerTagService#updateSex(java.lang.Integer, int)
	 */
	public void updateSex(Integer id, int code) {
		
		CustomerTag customerTag = genericDaoSupport.load(CustomerTag.class, id);
		customerTag.setSexTag(code);
		genericDaoSupport.update(customerTag);
		
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.client.service.CustomerTagService#updateAge(java.lang.Integer, int)
	 */
	public void updateAge(Integer id, int code) {
		CustomerTag customerTag = genericDaoSupport.load(CustomerTag.class, id);
		customerTag.setAgeTag(code);
		genericDaoSupport.update(customerTag);
		
	}

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.client.service.CustomerTagService#updateArea(java.lang.Integer, int)
	 */
	public void updateArea(Integer id, int code) {
		CustomerTag customerTag = genericDaoSupport.load(CustomerTag.class, id);
		customerTag.setAreaTag(code);
		genericDaoSupport.update(customerTag);
	}

}
